package com.mouse.haoi;

import com.mouse.haoi.entity.FileParam;
import com.mouse.haoi.service.CustomService;
import com.mouse.haoi.service.ImageService;
import com.mouse.haoi.utils.SequenceUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HaoiUtil {
    public static ImageService imageService = new ImageService();
    public static CustomService customService = new CustomService();

    /**
     * 上次图片
     *
     * @param param
     * @return
     */
    public static String sendFile(FileParam param) {
        Map<String, Object> parameterMap = new HashMap();
        parameterMap.put("UserStr", param.getUserStr());//密码串
        parameterMap.put("GameID", param.getGameID()); //题目编号，在好爱首页获取
        parameterMap.put("TimeOut", param.getTimeOut()); //超时时间 单位秒
        parameterMap.put("Rebate", param.getRebate());  //作者SOFTKEY
        parameterMap.put("Kou", "0");    //自定义扣分，一般写0
        parameterMap.put("BeiZhu", "");    //备注内容，会显示给好爱的员工
        parameterMap.put("Key", SequenceUtil.getTenRandom());//随机数
        String responseStr = null;
        if (param.getFilePath() != null && !"".equals(param.getFilePath())) {
            responseStr = imageService.sendFile(parameterMap, param.getFilePath());
        } else {
            responseStr = imageService.sendFile(parameterMap, param.getFileInputStream());
        }
        //验证码文件
        System.out.println("相应id:" + responseStr);
        return responseStr;
    }

    /**
     * 获取答案
     *
     * @return
     */
    public static String getAnswer(String id) {
        Map<String, Object> parameterMap = new HashMap();
        parameterMap.put("ID", id);    //题目流水号
        parameterMap.put("r", SequenceUtil.getSeqByRandom());    //随机数
        List<String> list = new ArrayList<>();
        customService.loop(20, () -> {
            String responseStr = imageService.getAnswer(parameterMap);
            if ("".equals(responseStr)) {
                return false;
            } else {
                list.add(responseStr);
                return true;
            }
        });
        System.out.println("验证码:" + list.get(0));
        return list.get(0);
    }

    /**
     * 获取题分【切记不要频繁调用】
     * {Host}/ GetPoint.aspx
     * User=abcde|241659339E7D986521&r=243wzxaw7qz
     *
     * @return
     */
    public static String getPoint(String UserStr) {
        Map<String, Object> parameterMap = new HashMap();
        parameterMap.put("User", UserStr);//密码串
        parameterMap.put("r", SequenceUtil.getSeqByRandom());    //随机数
        String responseStr = imageService.getPoint(parameterMap);
        System.out.println(responseStr);
        return responseStr;
    }
}
